let logger = require("pomelo-logger").getLogger(__filename);
let DateUtil = require("../../../utils/dateUtil");

module.exports = function (app) {
    return new Handler(app);
};

let Handler = function (app) {
    this.app = app;
};

Handler.prototype.exchangeCash = function (msg, session, callback) {
    let playerService = this.app.get("playerService");
    let player = session["player"];
    if (!player) {
        logger.error("set Description empty player");
        callback(null, {
            resultCode: 404,
            message: "玩家没有登陆"
        });
        return;
    }
    if (!msg.hasOwnProperty("exchangeType")) {
        logger.error("exchangeCash empty exchangeType");
        callback(null, {
            resultCode: 404,
            message: "type不能为空"
        });
        return;
    }
    if (!msg.hasOwnProperty("itemIndex")) {
        logger.error("exchangeCash empty itemIndex");
        callback(null, {
            resultCode: 404,
            message: "itemId不能为空"
        });
        return;
    }
    let exchangeType = msg.exchangeType;
    let itemIndex = msg.itemIndex;
    logger.info("exchangeCash with: " + msg.exchangeType);
    playerService.exchangeCash(player.getUid(), exchangeType, itemIndex, function (result){
        callback(null, result);
    });
};

Handler.prototype.setDescription = function (msg, session, callback) {
    let playerService = this.app.get("playerService");
    let player = session["player"];
    if (!player) {
        logger.error("set Description empty player");
        callback(null, {
            resultCode: 404,
            message: "玩家没有登陆"
        });
        return;
    }
    if (!msg.hasOwnProperty("description")) {
        logger.error("set Description empty description");
        callback(null, {
            resultCode: 404,
            message: "描述不能为空"
        });
        return;
    }
    callback(null, {
        resultCode: 0,
        message: "ok"
    });
    let description = msg["description"];
    player.setDescription(description);
    playerService.save(player);
    player.pushUpdateSelfAccount();
};

Handler.prototype.shareSuccess = function (msg, session, callback) {
    logger.info(`shareSuccess. frontendId=${session["frontendId"]} id=${session["id"]}`);
    let player = session["player"];
    if (!player) {
        logger.error("shareSuccess empty player");
        callback(null, {
            resultCode: 404,
            message: "玩家没有登陆"
        });
        return;
    }

    // let businessConfig = this.app.get("businessConfig");
    // let shareTimes = businessConfig.shareTimes || 1;
    // let shareAward = businessConfig.shareAward || 10;
    // let shareMailTitle = businessConfig.shareMailTitle || "分享奖励";
    // let shareMailContent = businessConfig.shareMailContent || "分享奖励";
    // logger.info(`businessConfig shareTimes: ${shareTimes}, shareAward:${shareAward}`);

    let configService = this.app.get("configService");
    let shareTimes = configService.getEnableConfigValueByKey("shareTimes") || 1;
    let shareAmount = configService.getEnableConfigValueByKey("shareAward") || 10;
    let shareMailTitle = configService.getEnableConfigValueByKey("shareMailTitle") || "分享奖励";
    let shareMailContent = configService.getEnableConfigValueByKey("shareMailContent") || "分享奖励";

    let now = DateUtil.nowMS();
    if(player.canShareToday(now, shareTimes)) {
        logger.info(`player ${player.getUid()} canShareToday`);
        player.addShareTime(now);
        let mailDataService = this.app.get("mailDataService");
        let playerService = this.app.get("playerService");

        let award = {currencyType : "diamond", amount : shareAmount};
        let sender = 0;
        let receiverUids = [];
        receiverUids.push(player.getUid());
        mailDataService.addMailData(shareMailTitle, 'shareSuccess', shareMailContent, award, sender, receiverUids, function (mailData) {
            if (!!mailData) {
                playerService.sendMail(mailData);
                logger.info("send mail ok");
            } else {
                logger.info("add mail failed");
            }
        });
    }
    callback(null, {resultCode: 0, message: "ok"});
};